<template>
  <div class="grouping-list table-theme">
    <head-title title="运营--分组列表"></head-title>
    <el-card>
      <div class="top-menu">
        <div class="menu-left"></div>
        <div class="menu-right">
          <refresh-button @refresh="refreshPage"></refresh-button>
          <search-view
            :option="searchOption"
            @value="goSearch"
            @clear="refreshPage"
          ></search-view>
          <div class="add">
            <el-button type="primary" @click="onOpenDialog('add')"
              >添加分组</el-button
            >
          </div>
        </div>
      </div>
      <p class="data-number">总数据数：{{ total }}</p>
      <el-table :data="list" border class="space-table">
        <el-table-column
          label="分组ID"
          align="center"
          prop="group_id"
          width="80"
        ></el-table-column>
        <el-table-column
          label="分组名称"
          align="center"
          prop="group_name"
        ></el-table-column>
        <el-table-column
          label="添加时间"
          align="center"
          prop="add_time"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="onOpenDialog('details', scope.row)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="primary"
              @click="onOpenDialog('edit', scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="onDel(scope.row.group_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pages">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="init"
          :current-page="page"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 添加空间弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="isDialog"
      width="500px"
      center
      :modal-append-to-body="false"
      @close="onClone('groupingForm')"
    >
      <el-form
        :model="groupingForm"
        :rules="groupingFormRules"
        ref="groupingForm"
        label-width="80px"
      >
        <el-form-item label="名称:" prop="group_name">
          <el-input
            v-model="groupingForm.group_name"
            placeholder="请输入分组名称"
            :disabled="type === 'details'"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClone('groupingForm')">取 消</el-button>
        <el-button
          type="primary"
          @click="onSubmit('groupingForm')"
          v-if="type !== 'details'"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "grouping_list",
  data() {
    return {
      searchOption: [
        {
          value: 0,
          label: "id",
        },
        {
          value: 1,
          label: "名称",
        },
      ],
      classSelect: 0,
      selectContent: "",
      limit: 10,
      page: 1,
      store_id: localStorage.getItem("store_id"),
      total: 0,
      list: [],
      title: "添加分组",
      type: "add",
      isDialog: false,
      groupingForm: {
        group_name: "",
      },
      groupingFormRules: {
        group_name: [
          {
            required: true,
            message: "请输入分组名称",
          },
        ],
      },
    };
  },
  created() {
    this.init(1);
  },
  methods: {
    init(e) {
      this.page = e;
      let data = {
        page: e ? e - 1 : 0,
        limit: 10,
        store_id: this.store_id,
      };
      if (this.classSelect == 0 && this.selectContent !== "") {
        data.group_id = this.selectContent;
      } else if (this.classSelect == 1 && this.selectContent !== "") {
        data.keyword = this.selectContent;
      }
      this.$http(this.$ApiList.StoreGroupList, data).then((res) => {
        this.list = res.datas.list;
        this.total = res.datas.count;
      });
    },
    // 刷新
    refreshPage() {
      this.page = 1;
      this.selectContent = "";
      this.init(1);
    },
    // 搜素
    goSearch(classSelect, selectContent) {
      this.classSelect = classSelect;
      this.selectContent = selectContent;
      this.init(1);
    },
    onOpenDialog(type, row) {
      this.type = type;
      switch (type) {
        case "add":
          this.title = "添加分组";
          break;
        case "edit":
        case "details":
          this.title = type=== 'edit'?"编辑分组":'分组详情';
          this.$http(this.$ApiList.StoreGroupInfo, {
            store_id: this.store_id,
            group_id: row.group_id,
          }).then((res) => {
            this.groupingForm = res.datas;
          });
          break;
        default:
          break;
      }
      this.isDialog = true;
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = this.groupingForm;
          data.store_id = this.store_id;
          switch (this.type) {
            case "add":
              delete data.group_id;
              this.$http(this.$ApiList.StoreGroupAdd, data).then((res) => {
                this.$message.success(res.datas);
                this.init(1);
                this.isDialog = false;
                this.$refs[formName].resetFields();
              });
              break;
            case "edit":
              this.$http(this.$ApiList.StoreGroupUpdate, data).then((res) => {
                this.$message.success(res.datas);
                this.init(this.page);
                this.isDialog = false;
                this.$refs[formName].resetFields();
              });
              break;
            default:
              break;
          }
        }
      });
    },
    // 删除
    onDel(group_id) {
      this.$confirm("此操作将永久删除该分组, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            group_id,
            store_id: this.store_id,
          };
          this.$http(this.$ApiList.StoreGroupDelete, data).then((res) => {
            this.$message.success(res.datas);
            this.init(this.page);
          });
        })
        .catch(() => {});
    },
    onClone(formName) {
      this.isDialog = false;
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.grouping-list {

}
</style>